package com.hujf.flink.worldcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @Author hujf
 * @Date 2022/3/17 14:15
 * @Description 使用将被淘汰的几口 ExecutionEnvironment
 */
public class BatchWorldCount {
    public static void main(String[] args) throws Exception {
        //1.创建运行环境
        ExecutionEnvironment env =  ExecutionEnvironment.getExecutionEnvironment();

        DataSet <String> text = env.fromElements("who is there , my name is hujf ,  who are you?");

        DataSet<Tuple2<String,Integer>> wordCount = text.flatMap(new LineSplitter()).groupBy(0)
                .sum(1);
        wordCount.print();
    }

    public static class LineSplitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
        @Override
        public void flatMap(String line, Collector<Tuple2<String, Integer>> out) {
            for (String word : line.split(" ")) {
                out.collect(new Tuple2<String, Integer>(word, 1));
            }
        }
    }
}
